package com.ny.dao.impl;

import com.ny.dao.ProductDao;
import com.ny.entity.Product;
import com.ny.until.DbUntil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements ProductDao {
    private DbUntil dbUntil = new DbUntil();
    @Override
    public List<Product> findAllProduct() throws SQLException, ClassNotFoundException {
        Connection connection = dbUntil.getCon();
        String sql = "SELECT * FROM products";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();

        List<Product> productList = new ArrayList<Product>();

        while (resultSet.next()){
            Product product = new Product();
            int id = resultSet.getInt("product_id");
            String pname = resultSet.getString("name");
            double price  = resultSet.getDouble("price");
            String stock = resultSet.getString("stock");
            String category = resultSet.getString("category");
            String description = resultSet.getString("description");

            product.setProductId(id);
            product.setName(pname);
            product.setPrice(price);
            product.setStock(stock);
            product.setCategory(category);
            product.setDescription(description);

            productList.add(product);
        }
        return productList;
    }
}
